//
//  HNActor.m
//  HeroNuts
//
//  Created by Magic on 4/2/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "HNActor.h"
#import "HNActorInfoCache.h"

@implementation HNActor

@synthesize leftSide = leftSide_;
@synthesize name = name_;
//@synthesize sprite = sprite_;
@synthesize speed = speed_;
@synthesize position = position_;
@synthesize hexSize = hexSize_;
@synthesize canFly = canFly_;
@synthesize shots = shots_;
//@synthesize count = count_;

-(id) init
{
	if ( (self = [super init]) ) {
	
        faceRight_ = YES;
		
		
	}
	return self;
}

- (void) dealloc
{
	[name_ release];
	[sprite_ release];
	[super dealloc];
}

-(NSComparisonResult) actorSpeedCompare:(HNActor *)aActor
{
	if (self.speed > aActor.speed) {
		return NSOrderedAscending;
	}
	else if(self.speed < aActor.speed){
		return NSOrderedDescending;			
	}
	else {
		return NSOrderedSame;
	}

}


- (void)setFaceRight:(BOOL)faceRight
{
    if (faceRight_ != faceRight) {
        self.sprite.scaleX *= -1;
        troopCountLabel_.scaleX *= -1;
        
        CGFloat offset = [[HNActorInfoCache sharedActorInfoCache] faceLeftOffsetXByActor:name_];
        if (faceRight) {
            offset = -offset;
        }
        CGPoint pos = self.sprite.position;
        self.sprite.position = ccp(pos.x+offset,pos.y);
        faceRight_ = faceRight;
    }
}

- (BOOL)faceRight
{
    return faceRight_;
}

- (void)setCount:(int)count
{
    count_ = count;
    if (troopCountLabel_)
    [troopCountLabel_ setString:[NSString stringWithFormat:@"%d",count]];
}

- (int)count
{
    return count_;
}

- (void)setSprite:(CCSprite *)sprite
{
    sprite_ = [sprite retain];
    troopCountBackground_ = [CCSprite spriteWithFile:@"troop_count.png"];
    troopCountBackground_.position = [[HNActorInfoCache sharedActorInfoCache] troopCountOffsetByActor:name_];
    [sprite_ addChild:troopCountBackground_];
    
    troopCountLabel_ = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d",count_] 
                                          fontName:@"Arial" fontSize:10];
    troopCountLabel_.position = troopCountBackground_.position;
    [sprite_ addChild:troopCountLabel_];
}

- (CCSprite *)sprite
{
    return sprite_;
}

@end
